home *** CD-ROM | disk | FTP | other *** search
Text File | 1986-06-28 | 2.8 KB | 118 lines | [TEXT/MACA] |
-
- ( Structures, Mach-1 version
- ----------
- Adding a structure compiler to Forth. JL 26.6.86.
- This file defines a Pascal-like 'record' structure;
- a record is a template for instances of the structure.
-
- Example
-
- :record a
- >long field1
- >word field2
- ;record
-
- myrec r1 \this creates an instance r1 of myrec whose fields
- may be accessed through myrec ^ field1 etc..
-
- for 'late binding' usage, the word ^field is provided)
-
- only forth also assembler
- decimal
- ( some MacForth definitions that Mach1 is missing )
- : =cells dup 2 mod + ;
- : needed depth 1- > abort" NEEDED- not enough stack items" ;
-
- CODE =string
- count rot count rot swap
- MOVE.W #0,-(A7)
- MOVE.L $C(A6),-(A7)
- MOVE.L $8(A6),-(A7)
- MOVE.W $6(A6),-(A7)
- MOVE.W $2(A6),-(A7)
- MOVE.W #12,-(A7)
- _pack6
- ADDQ.L #8,A6
- ADDQ.L #8,A6
- MOVE.W (A7)+,-(A6)
- MOVE.W #0,-(A6)
- RTS
- END-CODE
-
- CODE -string
- count rot count swap
- MOVE.L (A6)+,A0
- MOVE.L (A6)+,D0
- SWAP.W D0
- MOVE.L (A6)+,D1
- MOVE.W D1,D0
- MOVE.L (A6)+,A1
- _cmpstring
- MOVE.L D0,-(A6)
- RTS
- END-CODE
-
- ( do.record, creating one instance of a record ) ( 062686 jl )
- : do.record ( addr of master | -- )
- create dup ,
- begin dup c@ dup while ( not zero, i.e. end)
- 1+ =cells 4 + + ( next field in template )
- repeat
- drop 2+ w@ ( length stored here ) allot
- does> ( nothing special )
- ;
-
- ( :record ;record and friends) ( 062686 jl )
-
- : :record create 13579 4 does> do.record ;
-
- : ;record 2 needed
- 0 w, ( end of list) w, ( total length )
- 13579 = 0= abort" ;record without :record"
- ;
-
- : put.fieldname
- 32 word here over c@ 1+ dup =cells allot cmove ;
-
-
- ( field defining words ) ( 062686 jl )
- : >long ( addr | addr+4)
- put.fieldname dup w, 4 w, 4 + ;
- : >word ( addr | addr+2)
- put.fieldname dup w, 2 w, 2+ ;
- : >byte ( addr | addr+1)
- put.fieldname dup w, 1 w, 1+ ;
- : >bytes ( addr \ n | addr+n )
- put.fieldname over w, dup w, + ;
-
- ( ^field, addressing a field within a record ) ( 062686 jl )
- : ^field ( addr name | address of field )
- over @ ( addr name master )
- begin 2dup -string while ( no match )
- dup c@ 6 + =cells +
- dup c@ 0= ( end of list )
- abort" RECORD- specified field does not exist"
- repeat
- ( match found )
- dup c@ 1+ =cells + w@ ( start within record )
- swap drop + ( address of field )
- ;
-
- ( ^ ) ( 062686 jl )
-
- : ^ 32 word ^field ;
-
-
- ( example of a record structure ) ( 062686 jl )
-
- :record testrec
- >long date
- >long time
- >byte flag
- >word counts
- 30 >bytes description
- ;record
-
- testrec r1
- testrec r2
-